﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;

namespace YouTubeStormer
{
    public partial class Stats : UserControl
    {
        public class Values
        {
            public int Cycles { get; set; }
            public int NextCycle { get; set; }
            public int Logins { get; set; }
            public int LoginErrors { get; set; }
            public int Posted { get; set; }
            public int PostingErrors { get; set; }
            public int SpammedComments { get; set; }
            public int Skipped { get; set; }
            public int Votes { get; set; }
            public int VoteErrors { get; set; }
            public int AlreadyVotedGlitches { get; set; }
            public int DecaptchasComments { get; set; }
            public int DecaptchasCommentsFailed { get; set; }
            public int DecaptchasMain { get; set; }
            public int DecaptchasMainFailed { get; set; }
            public int WorkingClients { get; set; }
            public int CaptchaErrors { get; set; }
            public int ConnectionErrors { get; set; }
            public int HighestRatedSeen { get; set; }
            public int PendingRemoved { get; set; }
        }

        Timer refreshTimer = new Timer();
        DateTime startUp = DateTime.Now;
        WebClient webClient = new WebClient();
        public Values Statistics = new Values();

        public Stats()
        {
            InitializeComponent();
            refreshTimer.Interval = 500;
            refreshTimer.Tick += refreshTimer_Tick;
            refreshTimer.Start();
            UpdateStats();
        }

        void refreshTimer_Tick(object sender, EventArgs e)
        {
            UpdateStats();
        }

        void ApplyStatsToLabel(Label[] labels, string[] stats)
        {
            var leftSide = new string[stats.Length];
            var rightSide = new string[stats.Length];
            int i = 0;
            foreach (string statLine in stats)
            {
                var statLineParts = statLine.Split('~');
                leftSide[i] = statLineParts[1];
                rightSide[i] = statLineParts[0];
                i++;
            }

            string label1Text = string.Join(Environment.NewLine, leftSide);
            string label2Text = string.Join(Environment.NewLine, rightSide);
            if (labels[0].Text != label1Text) labels[0].Text = label1Text;
            if (labels[1].Text != label2Text) labels[1].Text = label2Text;
        }

        public void UpdateStats()
        {
            //new TimeSpan(0, 0, (int)DateTime.Now.Subtract(startUp).TotalSeconds) + " Running Time";

            labelNextCycle.Text = "Next Cycle: " + new TimeSpan(0, 0, Statistics.NextCycle).ToString();

            var leftSide = new string[]{
                "Working Clients~" + Statistics.WorkingClients,
                "Cycles Completed~" + Statistics.Cycles,
                "Connection Errors~" + Statistics.ConnectionErrors,
                "Skipped~" + Statistics.Skipped,
                "Logins~" + Statistics.Logins,
                "Login Errors~" + Statistics.LoginErrors,
                "Votes~" + Statistics.Votes,
                "Vote Errors~" + Statistics.VoteErrors,
                "Highest Rated Seen~" + Statistics.HighestRatedSeen,
            };

            var rightSide = new string[] {
                "Spammed Comments~" + Statistics.SpammedComments,
                "Posted~" + Statistics.Posted,
                "Posting Errors~" + Statistics.PostingErrors,
                "Captcha Service Errors~" + Statistics.CaptchaErrors,
                "Captchas (Main)~" + Statistics.DecaptchasMain,
                "Captchas (Main) Failed~" + Statistics.DecaptchasMainFailed,
                "Captchas (Comment)~" + Statistics.DecaptchasComments,
                "Captchas (Comment) Failed~" + Statistics.DecaptchasCommentsFailed,
                "Pending Removed~" + Statistics.PendingRemoved,
            };

            ApplyStatsToLabel(new Label[] { label1a, label1b }, leftSide);
            ApplyStatsToLabel(new Label[] { label2a, label2b }, rightSide);

        }
    }
}
